import '@/js/layui_exts/admin.js'

export function init() {
    layui.use(() => {
        const $ = layui.jquery, admin = layui.admin, form = layui.form, table = layui.table

        let dictGroupAmdType,selectedGroup;
        const reloadDictGroupTable = () => table.reloadData('dict-group-table', {
            where: {
                search: $('input[name=search]').val()
            }
        })
        const amdDictGroup = dictGroup => {
            dictGroupAmdType = dictGroup ? 'modify' : 'add'
            layer.open({
                type: 1,
                title: dictGroup ? ('<i class="layui-icon-edit layui-icon"></i>&nbsp;编辑字典组【' + dictGroup.name + '】') : ('<i class="layui-icon-add-circle layui-icon"></i>&nbsp;添加字典组'),
                offset: 'l',
                anim: 'slideRight',
                shade: 0.3,
                area: ['500px', '100%'],
                shadeClose: true,
                scrollbar: false,
                id: 'amd-dict-group-id',
                content: $('#amd-dict-group-content')
            })

            form.val('amd-dict-group-form-filter', dictGroup ?? {
                id: '',
                name: '',
                code: '',
                sort: '',
                desz: ''
            })
        }
        const rmDictGroup = dictGroup => admin.confirm('确定删除【' + dictGroup.name + '】吗？', () => admin.postM('sys/dict_group/remove', {id: dictGroup.id}, () => table.reload('dict-group-table')), '删除提示')
        table.render({
            elem: '#dict-group-table',
            url: '/api/sys/dict_group/page',
            headers: {
                'Authorization': admin.getToken()
            },
            parseData: ({code, msg, data}) => {
                let d = JSON.parse(admin.decrypt(data))
                return {
                    "code": code === 200 ? 0 : code,
                    "msg": msg,
                    "count": d.total,
                    "data": d.records
                }
            },
            toolbar: '#dict-group-table-toolbar',
            cols: [[{
                type: 'numbers',
                title: '序号'
            }, {
                field: 'name',
                title: '名称'
            }, {
                field: 'code',
                align: 'center',
                width: 100,
                title: '编码'
            }, {
                field: 'sort',
                align: 'center',
                width: 80,
                title: '排序'
            }, {
                field: 'desz',
                title: '说明'
            }, {
                title: '操作',
                align: 'center',
                width: 100,
                toolbar: '#dict-group-table-tools'
            }]],
            page: true,
            done: () => admin.perm()
        })

        table.on("tool(dict-group-table)", ({event, data}) => {
            switch (event) {
                case 'md':
                    amdDictGroup(data)
                    break;

                case 'rm':
                    rmDictGroup(data)
                    break;
            }
        })

        table.on("toolbar(dict-group-table)", ({event}) => {
            switch (event) {
                case 'add':
                    amdDictGroup()
                    break;

                case 'search':
                    reloadDictGroupTable()
                    break;

                case 'reset':
                    $('input[name=search]').val('')
                    reloadDictGroupTable()
                    break;
            }
        })

        table.on('row(dict-group-table)', o=>{
            let data = o.data
            selectedGroup=data
            $('#current-dict-group').text(data.name+'【字典组】')
            reloadDictItemTable()
            o.setRowChecked({
                type: 'radio'
            })
        });

        $('input[name=search]').keydown(e => {
            if (e.keyCode === 13)
                reloadDictGroupTable()
        })

        form.on('submit(submit-amd-dict-group-form)', ({field}) => {
            admin.postM('sys/dict_group/' + dictGroupAmdType, field, () => {
                layer.closeAll('page')
                reloadDictGroupTable()
            })
            return false
        })

        /*以下为字典项*/
        let dictItemAmdType

        const reloadDictItemTable = () => table.reloadData('dict-item-table', {
            where: {
                id:selectedGroup.id
            }
        })
        const  amdDictItem = dictItem => {
            if (!selectedGroup) {
                layer.msg('请先选择字典组')
                return false
            }
            dictItemAmdType = dictItem ? 'modify' : 'add'
            layer.open({
                type: 1,
                title: dictItem ? ('<i class="layui-icon-edit layui-icon"></i>&nbsp;编辑字典项【' + dictItem.name + '】') : ('<i class="layui-icon-add-circle layui-icon"></i>&nbsp;添加字典项'),
                offset: 'r',
                anim: 'slideLeft',
                shade: 0.3,
                area: ['500px', '100%'],
                shadeClose: true,
                scrollbar: false,
                id: 'amd-dict-item-id',
                content: $('#amd-dict-item-content')
            })
            form.val('amd-dict-item-form-filter', dictItem ?? {id: '',groupId:selectedGroup.id, name: '', code: '', sort: '', desz: ''})
        }
        const rmDictItem = dictItem => admin.confirm('确定删除【' + dictItem.name + '】吗？', () => admin.postM('sys/dict_item/remove', {id: dictItem.id}, () => reloadDictItemTable()), '删除提示')

        table.render({
            elem: '#dict-item-table',
            url: '/api/sys/dict_item/list',
            where: {
                id: new Date().getTime()
            },
            headers: {
                'Authorization': admin.getToken()
            },
            parseData: ({code, msg, data}) => {
                let d = JSON.parse(admin.decrypt(data))
                return {
                    "code": code === 200 ? 0 : code,
                    "msg": msg,
                    "count": d.length,
                    "data": d
                }
            },
            toolbar: '#dict-item-table-toolbar',
            cols: [[{
                type: 'numbers',
                title: '序号'
            }, {
                field: 'name',
                title: '名称'
            }, {
                field: 'code',
                align: 'center',
                title: '编码'
            }, {
                field: 'sort',
                align: 'center',
                width: 100,
                title: '序号'
            }, {
                field: 'desz',
                title: '说明'
            }, {
                title: '操作',
                align: 'center',
                width: 300,
                toolbar: '#dict-item-table-tools'
            }]],
            done: () => admin.perm()
        })

        table.on("tool(dict-item-table)", ({event, data}) => {
            switch (event) {
                case 'md':
                    amdDictItem(data)
                    break;

                case 'rm':
                    rmDictItem(data)
                    break;
            }
        })

        table.on("toolbar(dict-item-table)", ({event}) => {
            switch (event) {
                case 'add':
                    amdDictItem()
                    break;
            }
        })

        form.on('submit(submit-amd-dict-item-form)', ({field}) => {
            admin.postM('sys/dict_item/' + dictItemAmdType, field, () => {
                layer.closeAll('page')
                reloadDictItemTable()
            })
            return false
        })
    })

    return {};
}
